<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="5" :sm="15">
            <a-form-item label="资料编号">
              <a-input placeholder="请输入资料编号" v-model="queryParam.driverid"/>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item label="司机姓名">
              <a-input placeholder="请输入司机姓名" v-model="queryParam.driverName"/>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item label="身份证号码">
              <a-input placeholder="请输入身份证号码" v-model="queryParam.cardid"/>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item label="司机电话">
              <a-input placeholder="请输入司机电话" v-model="queryParam.phoneno"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button v-if="addEnable" type="primary" icon="plus" @click="$refs.modal.add()">添加</a-button>
      <a-dropdown v-if="removeEnable&&selectedRowKeys.length > 0">
        <a-button type="danger" icon="delete" @click="delByIds(selectedRowKeys)">删除</a-button>
      </a-dropdown>
      <a-upload
        v-if="addEnable"
        name="file"
        :action="uploadUrl"
        :headers="headers"
        :showUploadList="false"
        @change="uploadChange"
      >
        <!-- <a-button icon="upload"> 导入</a-button> -->
      </a-upload>
    </div>
    <div>
      <a-tabs default-active-key="0" @change="callback">
        <a-tab-pane key="0" tab="全部">

        </a-tab-pane>
        <a-tab-pane key="1" tab="已审核">

        </a-tab-pane>
        <a-tab-pane key="2" tab="待审核">

        </a-tab-pane>
        <a-tab-pane key="3" tab="已驳回">

        </a-tab-pane>
      </a-tabs>
    </div>
    <div>
      <a-modal
        title="提示"
        :visible="visible"
        :confirm-loading="confirmLoading"
        @ok="jinyongOk()"
        @cancel="jinyongCancel"
      >
        <p>{{ ModalText }}</p>
      </a-modal>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="driverid"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :columns="columns"
      :data="loadData"
    >
      <span slot="action" slot-scope="text, record">
        <a v-if="editEnabel" @click="handleEdit(record)">修改</a>
        <a-divider v-if="record.driverStt===1" type="vertical" />
        <!-- <a v-if="record.driverStt===0" @click="delByIds([record.driverid])">删除</a> -->
        <a v-if="record.driverStt===1" @click="checkDriver(record)">审核</a>
        <a-divider type="vertical" v-if="record.driverStt===1||record.driverStt===0 " />
        <a v-if="record.driverStt===0&&record.bak1==='0'" @click="showModal(record)">禁用</a>
        <a v-if="record.driverStt===0&&record.bak1==='1'" @click="showModal(record)">启用</a>
        <a v-if="record.driverStt===1" @click="rejectDriver(record)">驳回</a>
      </span>
    </s-table>
    <driverInfo-modal ref="modal" @ok="handleOk"/>
    <reject-modal ref="remodal" @ok="handleOk"/>
    <drivercheck-modal ref="checkmodal" @ok="handleOk"/>
  </a-card>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { STable } from '@/components'
import { getDriverInfoList, delDriverInfo, EnableDisabled } from '@/api/shifts/driverinfo'
import DriverInfoModal from './modules/DriverInfoModal.vue'
import RejectModal from './modules/RejectModal.vue'
import drivercheckModal from './modules/DriverCheckModal.vue'
// import { checkPermission } from '@/utils/permissions'

export default {
  name: 'TableList',
  components: {
    STable,
    DriverInfoModal,
    RejectModal,
    drivercheckModal
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
      },
      // 表头
      columns: [
        {
          title: '资料编号',
          dataIndex: 'driverid'
        },
        {
          title: '司机姓名',
          dataIndex: 'driverName'
        },
        {
          title: '身份证号码',
          dataIndex: 'cardid'
        },
        {
          title: '司机电话',
          dataIndex: 'phoneno'
        },
        {
          title: '操作',
          width: '200px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getDriverInfoList(Object.assign(parameter, this.queryParam))
      },
      selectedRowKeys: [],
      selectedRows: [],
      previewVisible: false,
      uploadUrl: process.env.VUE_APP_API_BASE_URL + '/system/oss/upload',
      headers: {
        token: Vue.ls.get(ACCESS_TOKEN)
      },
      addEnable: 'true',
      editEnabel: 'true',
      removeEnable: 'true',
      stopButton: '',
      ModalText: '',
      visible: false,
      confirmLoading: false,
      driverid: '',
      bak1: ''
    }
  },
  filters: {
  },
  created () {
  },
  methods: {
    // 禁用
    showModal (record) {
      this.driverid = record.driverid
      this.bak1 = record.bak1
      if (this.bak1 === '0') {
        this.ModalText = '此操作将禁用该司机的报班功能, 是否继续?'
      } else {
        this.ModalText = '此操作将启用该司机的报班功能, 是否继续?'
      }
      this.visible = true
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleAdd () {
      this.$refs.modal.add()
    },
    // 修改司机信息
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    // 驳回司机信息
    rejectDriver (record) {
      this.$refs.remodal.add(record)
      // this.$refs.modal
      // this.modal.RejectModel()
      // dispatch(RejectModel.push('/'))
      // this.$refs.remodal.add(record)
    },
    checkDriver (record) {
      this.$refs.checkmodal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh(true)
    },
    uploadChange (info) {
      if (info.file.status !== 'uploading') {
      }
      if (info.file.status === 'done') {
        this.$refs.table.refresh(true)
      }
    },
    delByIds (ids) {
      delDriverInfo({ ids: ids.join(',') }).then(res => {
        if (res.code === 0) {
          this.$message.success('删除成功')
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
        this.selectedRowKeys = []
      })
    },
    // tab切换是否审核
    callback (key) {
      if (key === '0') {
        this.queryParam.driverStt = ''
        this.$refs.table.refresh(true)
      } else if (key === '1') {
        this.queryParam.driverStt = '0'
        this.$refs.table.refresh(true)
      } else if (key === '2') {
        this.queryParam.driverStt = '1'
        this.$refs.table.refresh(true)
      } else if (key === '3') {
        this.queryParam.driverStt = '4'
        this.$refs.table.refresh(true)
      }
    },
    // 禁用
    jinyongOk () {
      if (this.bak1 === '0') {
        EnableDisabled({ driverID: this.driverid, Bak1: '1' }).then(res => {
          if (res.code === 0) {
            this.$message.success('禁用成功')
            this.handleOk()
          } else {
            this.$message.error(res.msg)
          }
          this.selectedRowKeys = []
        })
        this.ModalText = '正在禁用中......'
        this.confirmLoading = true
        setTimeout(() => {
          this.visible = false
          this.confirmLoading = false
        }, 1000)
      } else {
        EnableDisabled({ driverID: this.driverid, Bak1: '0' }).then(res => {
          if (res.code === 0) {
            this.$message.success('启用成功')
            this.handleOk()
          } else {
            this.$message.error(res.msg)
          }
          this.selectedRowKeys = []
        })
        this.ModalText = '正在启用中......'
        this.confirmLoading = true
        setTimeout(() => {
          this.visible = false
          this.confirmLoading = false
        }, 1000)
      }
    },
    jinyongCancel (e) {
      this.visible = false
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
<style scoped>
</style>
